﻿/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Windows.Forms;
using PluginInterface;
using Qurihara;
using System.Collections;
using System.Drawing;
using Qurihara.QBasic;

using System.Reflection;
using System.IO;

namespace KotodamaPlugin.ViewWritePlugin
{
	public class ViewEraseMode : Mode
	{
        public event StrokeElementErasedEventHandler StrokeElementErased = null;
        public delegate void StrokeElementErasedEventHandler(object sender, StrokeElementErasedEventArgs e);
        public class StrokeElementErasedEventArgs : EventArgs
        {
            public StrokeElementErasedEventArgs(StrokeElement se)
            {
                StrokeElement = se;
            }
            public StrokeElement StrokeElement;
        }

        //internal ToolBarButton button;

		protected FigureElementList subList = new FigureElementList();
		protected FigureElementList subList2 = new FigureElementList();
        protected FigureElementList _selected = new FigureElementList();

		protected FigureElementList fel = new FigureElementList();
		protected System.Windows.Forms.MouseEventHandler me;

		protected bool dragInFlag = false;
		protected IKotodamaBase kotodama;
        protected bool strokeErased = false;

		public ViewEraseMode(IKotodamaBase k)//,ToolBarButton b)
		{
            kotodama = k;
			me = new MouseEventHandler(MouseMove);
            //button = b;
		}
		protected override void Init()
		{
			kotodama.Control.MouseDown += new MouseEventHandler(MouseDown);
			kotodama.Control.MouseUp += new MouseEventHandler(MouseUp);
			kotodama.Control.MouseMove += me;

			Assembly  assm = Assembly.GetExecutingAssembly();
			Stream stream = assm.GetManifestResourceStream("ViewWritePlugin.Eraser.cur");
			kotodama.Control.Cursor = new System.Windows.Forms.Cursor(stream);

			//kotodama.Control.Cursor = new System.Windows.Forms.Cursor(GetType(), "Eraser.cur");
            //button.Pushed =true;
		}
		protected override void End()
		{
			kotodama.Control.MouseDown -= new MouseEventHandler(MouseDown);
			kotodama.Control.MouseUp -= new MouseEventHandler(MouseUp);

			kotodama.Control.MouseMove -= me;
			kotodama.Control.Cursor = System.Windows.Forms.Cursors.Default;
            //button.Pushed = false;

			kotodama.SelectedElementList.Highlight = false;
			kotodama.SelectedElementList.Clear();

		}

		protected void MouseDown(object o, System.Windows.Forms.MouseEventArgs e)
		{
			if (!dragInFlag)
			{
				dragInFlag = true;
                strokeErased = false;
			}
			fel.Clear();
		}
		protected void MouseUp(object o, System.Windows.Forms.MouseEventArgs e)
		{
			if (dragInFlag)
			{
                if (kotodama.SelectedElementList.Count>0)
                {
                    //Strokeだけ消せる
                    //kotodama.Presentation.CurrentSlide.EraseObjects(kotodama.SelectedElementList);
                    kotodama.SelectedElementList.Clear();
                }
				dragInFlag = false;
			}
		}
		protected void MouseMove(object o, System.Windows.Forms.MouseEventArgs e)
		{
			if (dragInFlag)
			{
				kotodama.SelectedElementList.Highlight = false;
				kotodama.SelectedElementList.Clear();

				subList.Clear();
				Point p = new Point(e.X,e.Y);
				kotodama.Presentation.CurrentSlide.View.ScreenToView(ref p);

				GetPointedElement(p);//,ref subList);

				if (subList.Count >0) 
				{

                    GetShallowest();
                    foreach (object obj in subList)
                    {
                        if (obj.GetType() == typeof(Qurihara.StrokeElement))
                        {
                            //いきなり消さずに、自分が消せるストロークかどうか確認する
                            StrokeElement se = (StrokeElement)obj;

                            string name = (string)kotodama.EnvironmentTable["ComForDisplay.Name"];

                            if (se.Tag == "" || se.Tag.Equals("owner=" + name))
                            {
                                kotodama.Presentation.CurrentSlide.EraseObjects(subList);
                                strokeErased = true;
                                if (StrokeElementErased != null) StrokeElementErased(this, new StrokeElementErasedEventArgs((StrokeElement)obj));
                            }
                        }
                        else
                        {
                            if (!strokeErased)
                            {
                                //subList.Highlight = true;
                                //Strokeだけ消せる
                                //kotodama.SelectedElementList.Add(subList);
                            }
                        }
                    }
					subList.Clear();
				}

			}
		}
		protected void GetPointedElement(Point p)//,ref FigureElementList list)
		{
			PointF pf = DrawingUtil.GetRelativePoint(kotodama.Presentation.CurrentSlide.View.ViewRectangle,p);
			QArrayList qa = (QArrayList)subList2;
			kotodama.Presentation.CurrentSlide.OnScreenGrid.GetPointedGridList(pf.X,pf.Y,ref qa);
			subList2.getPointedElementList(p.X,p.Y,ref subList,false);
			subList2.Clear();
		}
		private static FigureElementPriorityComparer priorityComparer = new FigureElementPriorityComparer();
		protected void GetShallowest()
		{
			subList.Sort(priorityComparer);
			object fe = subList[subList.Count-1];
			subList.Clear();
			subList.Add(fe);
		}
	}
}